<template>
	<joy-page class="">
		<image class="top" :src="bgImg"></image>
		<view style="position: relative;">
			<m-company :list="list" @click="detail"></m-company>
			<view class="load-more-box">
				<uni-load-more v-if="status == '请求中'" status="正在加载..." :showIcon="true"></uni-load-more>
				<uni-load-more v-if="status == '没有更多'" status="没有更多了" :showIcon="false"></uni-load-more>
				<uni-load-more v-if="status == '暂无数据'" status="暂无数据" :showIcon="false"></uni-load-more>
				<uni-load-more v-if="status == '请求失败'" status="加载失败，点我重试" :showIcon="false" @click="reLoad"></uni-load-more>
			</view>
		</view>
	</joy-page>
</template>

<script>
import { mapState, mapGetters } from 'vuex';
import mSwiper from '@/components/m-swiper/m-swiper.vue';
import mCompany from '@/components/m-company/m-company.vue';
export default {
	components: {
		mSwiper,
		mCompany,
	}, 
	computed: {
		...mapState(['forcedLogin']),
		...mapGetters(['hasLogin'])
	},
	data() {
		return {
			list: [],
			page: 0,
			limit: 10,
			status:"",
			bgImg:"https://tse1-mm.cn.bing.net/th/id/OIP-C.dRlFmZF9UuLo9CujKI8M4AHaD_?w=337&h=181&c=7&r=0&o=5&dpr=1.3&pid=1.7"
		};
	},
	async onLoad(query) {
		this.getList();
	},
	
	onReachBottom() {
		this.page++;
		this.getList();
	},
	
	methods: {
		async getList() {
			let param = {
				page: this.page,
				count: this.limit,
				keyword:""
			};
			this.status = '请求中';
			let res = await this.$apis.getCompanyList(param);
			if (res) {
				let data = res.data.items
				this.list = this.list.concat(data || []);
				this.changeStatus(res);
			}
		},
		
		// 修改请求状态
		changeStatus(data) {
			if (this.list.length === 0) {
				this.status = '暂无数据';
			} else if (this.page >= Math.ceil(data.total / this.limit)) {
				this.status = '没有更多';
			} else {
				this.status = '请求更多';
			}
		},
		
		detail(item) {
			console.log("传过去了，公司id",item.id)
			this.$mRouter.push({
				route: this.$mRoutesConfig.companyDetail,
				query: {
					id: item.id
				}
			});
		},
	}
};
</script>

<style lang="scss" scoped>
.top{
	height: 320upx;
	width: 100%;
}
</style>
